/*
 * author: hujingfei914@msn.com
 * date: 2010-10-15
 * description: the implementation file of 3ds aircraft
 */

#include "..\StdAfx.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include "3DSAircraft.h"

C3DSAircraft::C3DSAircraft(
 float x, float y, float z,
 double xrot, double yrot, double zrot)
{
	x_ = x;
	y_ = y;
	z_ = z;

	xrot_ = originalXRot = xrot;
	yrot_ = originalYRot = yrot;
	zrot_ = originalZRot = zrot;

	numberOfObjects = 0;
}

C3DSAircraft::~C3DSAircraft(void)
{
}

void C3DSAircraft::draw(void)
{
	
	glPushMatrix();
		glPushMatrix();
		//glDisable(GL_BLEND);
		glColor3f(1.0f, 1.0f, 1.0f);
		glTranslatef(x_, y_, z_);
		// To make the aircraft's coordinate system same as the test environment's coordinate system.
		// First rotate around the x-axis, means pitch
		glRotatef(xrot_, 1.0f, 0.0f, 0.0f);
		// Second rotate around the y-axis, means heading
		glRotatef(yrot_, 0.0f, 1.0f, 0.0f);		
		// Third rotate around the z-axis, means roll
		glRotatef(zrot_, 0.0f, 0.0f, 1.0f);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glEnable(GL_DEPTH_TEST);
		
		int n_index;
		for (n_index = 0; n_index < numberOfObjects; ++n_index) {
			glEnable(GL_TEXTURE_2D);
			glBindTexture(GL_TEXTURE_2D, objects[n_index].id_texture);
			glBegin(GL_TRIANGLES);
			int l_index;
				for (l_index = 0; l_index < objects[n_index].polygons_qty; l_index++)
				{
					//----------------- FIRST VERTEX -----------------
					// Texture coordinates of the first vertex
					glTexCoord2f( objects[n_index].mapcoord[ objects[n_index].polygon[l_index].a ].u,
								  objects[n_index].mapcoord[ objects[n_index].polygon[l_index].a ].v);
					// Coordinates of the first vertex
					glVertex3f( objects[n_index].vertex[ objects[n_index].polygon[l_index].a ].x,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].a ].y,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].a ].z); //Vertex definition

					//----------------- SECOND VERTEX -----------------
					// Texture coordinates of the second vertex
					glTexCoord2f( objects[n_index].mapcoord[ objects[n_index].polygon[l_index].b ].u,
								  objects[n_index].mapcoord[ objects[n_index].polygon[l_index].b ].v);
					// Coordinates of the second vertex
					glVertex3f( objects[n_index].vertex[ objects[n_index].polygon[l_index].b ].x,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].b ].y,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].b ].z);
			        
					//----------------- THIRD VERTEX -----------------
					// Texture coordinates of the third vertex
					glTexCoord2f( objects[n_index].mapcoord[ objects[n_index].polygon[l_index].c ].u,
								  objects[n_index].mapcoord[ objects[n_index].polygon[l_index].c ].v);
					// Coordinates of the Third vertex
					glVertex3f( objects[n_index].vertex[ objects[n_index].polygon[l_index].c ].x,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].c ].y,
								objects[n_index].vertex[ objects[n_index].polygon[l_index].c ].z);
				}
			glEnd();
		}
		//glEnable(GL_BLEND);
		glPopMatrix();
	glPopMatrix();
}


/*
 * Update the aircraft model's position and orientation
 */
void C3DSAircraft::updatePositionAndOrientation(UINT nFlags, CPoint point)
{
	static GLfloat m_fLastX = (GLfloat)point.x;
	static GLfloat m_fLastY = (GLfloat)point.y;

	int diffX = (int)(point.x - m_fLastX);
	int diffY = (int)(point.y - m_fLastY);

	m_fLastX = (GLfloat)point.x;
	m_fLastY = (GLfloat)point.y;

	// Left mouse button
	if (nFlags & MK_LBUTTON) 
	{
		xrot_ += (float)0.5f * diffY;
		if ((xrot_ > 360.0f) || (xrot_ < -360.0f)) 
		{
			xrot_ = 0.0f;
		}

		yrot_ += (float)0.5f * diffX;
		if ((yrot_ > 360.0f) || (yrot_ < -360.0f)) 
		{
			yrot_ = 0.0f;
		}
	}

	// Right mouse button
	else if (nFlags & MK_RBUTTON) 
	{
		z_ -= (float)0.5f * diffY;
	}
	
	// Middle mouse button
	else if (nFlags & MK_MBUTTON) {
		x_ += (float)0.5f * diffX;
		y_ -= (float)0.5f * diffY;
	}

}

void C3DSAircraft::setTextureId(int index, unsigned int texId)
{
	objects[index].id_texture = texId;
}

int C3DSAircraft::getTextureId(int index)
{
	return objects[index].id_texture;
}

/*
 * Update the aircraft model's position and orientation
 */
void C3DSAircraft::updatePositionAndOrientation(const FlyState& flyState)
{
	xrot_ = originalXRot + flyState.theta;	// Pitch
	yrot_ = originalYRot + flyState.phi;		// Roll
	zrot_ = originalZRot + flyState.psi;		// Heading
}